<template>
  <div class="login">
    <div class="loginbox">
      <h1>外卖管理系统登录</h1>
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="loginForm"
        class="demo-loginForm"
        size="mini"
      >
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            autocomplete="off"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input
            :type="flag ? 'text' : 'password'"
            v-model="loginForm.pwd"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
            @keyup.native.13.once="submitForm('loginForm')"
          >
            <template #suffix>
              <i
                class="iconfont"
                :class="flag ? 'icon-eyes-' : 'icon-eyes'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('loginForm')"
            :loading="isload"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validateUserName, validatePwd } from '../utils/validate.js'
import { login } from '../api/user.js'
import local from '../utils/local.js'
import { createRoute } from '../router/index.js'

export default {
  data() {
    return {
      flag: false,
      isload: false,
      loginForm: {
        username: '',
        pwd: ''
      },
      rules: {
        username: [{ validator: validateUserName, trigger: 'blur' }],
        pwd: [{ validator: validatePwd, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const data = await login({
            account: this.loginForm.username,
            password: this.loginForm.pwd
          })
          if (data.code === 0) {
            this.isload = true
            setTimeout(() => {
              local.set('token', data.token)
              local.set('role', data.role)
              createRoute()
              this.$router.push('/')
            }, 2000)
          } else {
            this.isload = false
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background-color: #2d3a4b;
  display: flex;
  justify-content: center;
  align-items: center;
  .loginbox {
    width: 400px;
    height: 300px;
    h1 {
      padding: 20px 0;
      text-align: center;
      font-size: 20px;
      letter-spacing: 2px;
      color: #fff;
    }
    button {
      width: 100%;
    }
    /deep/ .el-input {
      input {
        background-color: #2d3a4b;
        color: #fff;
      }
      .el-input__suffix-inner {
        float: right;
      }
    }
  }
}
</style>
